<template>
  <div>
    <div class="header">
      <div class="header-left">
        <i class="el-icon-s-fold" @click="handleIsCollapse"></i>
        <img src="../static/image/logoLeft.png" alt="" style="margin-top:10px;height:40px;cursor: pointer;" @click="handleGoHome" />
        <!-- 奥星制药 -->
      </div>

      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <div class="page-header-user el-dropdown-link">
            <span style="vertical-align:16px;margin-right:10px;font-size:18px;color:#fff;line-hight:35px;">{{userName}}</span>
            <img src="../static/image/tou.png" alt="" class="page-header-avatar">
            <i class="el-icon-caret-bottom"></i>
          </div>

          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="logout">{{$t('menu.log_out_and_log_in')}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>

      <!-- 隐藏国际化 -->
      <!-- <div class="header-right" style="margin-top:10px;margin-right:25px;">
        <el-dropdown @command="handleCommand">
          <div class="page-header-user el-dropdown-link">
            <span style="vertical-align:16px;margin-right:10px;font-size:18px;color:#fff;">{{curLang}}</span>
            <i class="el-icon-caret-bottom"></i>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="langCn">中文简体</el-dropdown-item>
            <el-dropdown-item command="langTw">中文繁體</el-dropdown-item> -->
      <!-- <el-dropdown-item command="langEn">English</el-dropdown-item> -->
      <!-- </el-dropdown-menu>
        </el-dropdown> -->
      <!-- </div> -->

    </div>
    <div class="main-container">
      <div class="content-left" v-if="menuLeftShow">
        <menuLeft :isCollapse="isCollapse" @close="handleColse" :screenWidth="screenWidth" v-if="userName"></menuLeft>
      </div>
      <div class="content-right" v-if="userName">
        <transition name="fade" mode="out-in">
          <router-view></router-view>
        </transition>
      </div>
    </div>

  </div>
</template>

<script>
import menuLeft from "../components/menuLeft.vue"
import { userAdmin } from "@/api/userAdmin"

import { setup, getCurLange } from "../assets/language";

export default {
  data () {
    return {
      isCollapse: false,
      userName: "",
      menuLeftShow: false,
      screenWidth: document.body.clientWidth, // 初始化屏幕宽度
      curLang: '',
    }
  },
  components: {
    menuLeft
  },
  created () {
    if (this.$route.query.batchtype == 1) {
      this.menuLeftShow = false
    } else {
      this.menuLeftShow = true
    }

    if (localStorage.getItem('userName') == null) {
      userAdmin.initLogin()
    }
    this.currentUser()
    this.curLang = getCurLange()
  },
  mounted () {
    this.updateScreenSize();
    window.addEventListener('resize', this.updateScreenSize);
  },
  beforeUnmount () {
    // 组件卸载前移除事件监听器，避免内存泄漏
    window.removeEventListener('resize', this.updateScreenSize);
    window.localStorage.clear()
  },

  methods: {
    updateScreenSize () {
      this.screenWidth = document.documentElement.clientWidth || document.body.clientWidth;
      // 根据需要处理窗口尺寸变化后的逻辑
    },
    async handleCommand (command) {
      if (command == 'logout') {
        // window.localStorage.clear()
        // window.sessionStorage.clear()
        if (this.$route.query.type != 1) {
          this.$router.push("/home");
        } else {
          window.location.href = `${window.location.origin}/#/batchInfo?batchtype=1&`
        }
        await userAdmin.logout()
        setTimeout(() => {
          this.$keycloak.logoutFn()
        }, 100);
      } else if (command == 'langCn') {
        setup("zh");
        location.reload()
      } else if (command == 'langEn') {
        setup("en");
        location.reload()
      } else if (command == 'langTw') {
        setup("tw");
        location.reload()
      }

    },
    handleColse (val) {
      this.isCollapse = val
    },
    handleIsCollapse () {
      this.isCollapse = !this.isCollapse
    },
    currentUser () {
      userAdmin.currentUser().then(res => {
        if (res.code == 200) {
          this.userName = res.data.name
          localStorage.setItem('userName', this.userName)
        }
      })
    },
    handleGoHome () {
      this.$router.push("/home");
    },
  }
}
</script>

<style scoped>
.fade-enter {
  opacity: 0;
}
.fade-enter-active {
  transition: opacity 0.3s;
}
.fade-leave-to {
  opacity: 0;
}
.fade-leave-active {
  transition: opacity 0.3s;
}
.header {
  position: relative;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 100%;
  height: 60px;
  font-size: 22px;
  color: #fff;
  background-color: #3b2e58;
  /* background: #A349A4; */
  padding: 0 30px;
  overflow: hidden;
}
.header .header-left {
  display: inline-block;
  letter-spacing: 2px;
  line-height: 70px;
}
.el-icon-s-fold {
  cursor: pointer;
  margin-right: 10px;
  vertical-align: 12px;
}
.header .header-right {
  float: right;
  padding: 7px 0;
}
.page-header-avatar {
  width: 45px;
  height: 45px;
  padding: 5px;
  border-radius: 50%;
}
.header .header-right .el-icon-caret-bottom {
  vertical-align: 16px;
  color: #ffffff;
  margin-left: 10px;
}
.main-container {
  height: calc(100vh - 60px);
  overflow: auto;
  display: flex;
}
.content-right {
  padding: 0px 25px 15px 25px;
  flex: 1;
  width: calc(100vw - 200px);
  overflow: auto;
  position: relative;
}
.content-left {
  background: #434a50 !important;
}
</style>
